package com.ds.lens.data.service.dao;

import com.ds.lens.data.service.dao.entity.LensMetric;
import com.ds.lens.data.service.dao.sql.provider.LensMetricSqlProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Description:
 *
 * @author WeiShaoying
 * @date 2019-11-05
 */
@Repository
public interface LensMetricDao {

    String COLUMNS = "id, integration_id, name, units, description, created_time, mender, modified_time ";

    @Delete({
            "delete from lens_metric",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into lens_metric (",
            COLUMNS,
            ") values (#{id,jdbcType=BIGINT}, #{integrationId,jdbcType=BIGINT}, ",
            "#{name,jdbcType=VARCHAR}, #{units,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, ",
            "#{createdTime,jdbcType=TIMESTAMP}, #{mender,jdbcType=VARCHAR}, ",
            "#{modifiedTime,jdbcType=TIMESTAMP})"
    })
    int insert(LensMetric record);

    @InsertProvider(type = LensMetricSqlProvider.class, method = "insertSelective")
    int insertSelective(LensMetric record);

    @Select({
            "select",
            COLUMNS,
            "from lens_metric",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results(id = "lensMetric", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "integration_id", property = "integrationId", jdbcType = JdbcType.BIGINT),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "units", property = "units", jdbcType = JdbcType.VARCHAR),
            @Result(column = "description", property = "description", jdbcType = JdbcType.VARCHAR),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "mender", property = "mender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "modified_time", property = "modifiedTime", jdbcType = JdbcType.TIMESTAMP)
    })
    LensMetric selectByPrimaryKey(Long id);

    @UpdateProvider(type = LensMetricSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(LensMetric record);

    @Update({
            "update lens_metric",
            "set integration_id = #{integrationId,jdbcType=BIGINT},",
            "name = #{name,jdbcType=VARCHAR},",
            "units = #{units,jdbcType=VARCHAR},",
            "description = #{description,jdbcType=VARCHAR},",
            "created_time = #{createdTime,jdbcType=TIMESTAMP},",
            "mender = #{mender,jdbcType=VARCHAR},",
            "modified_time = #{modifiedTime,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(LensMetric record);

    @Select({"<script>",
            "select",
            COLUMNS,
            "from lens_metric",
            "<if test='lensMetric != null'>",
            "where 1 = 1",
            "<if test='lensMetric.id != null'> and id = #{lensMetric.id,jdbcType=BIGINT}</if>",
            "<if test='lensMetric.integrationId != null'> and integration_id = #{lensMetric.integrationId,jdbcType=BIGINT}</if>",
            "<if test='lensMetric.name != null'> and name = #{lensMetric.name,jdbcType=VARCHAR}</if>",
            "<if test='lensMetric.units != null'> and units = #{lensMetric.units,jdbcType=VARCHAR}</if>",
            "</if>",
            "</script>"
    })
    @ResultMap("lensMetric")
    List<LensMetric> queryMetricsByCondition(@Param("lensMetric") LensMetric lensMetric);
}
